﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MenuAdd.aspx.cs" Inherits="ProjectStudy.MenuManage.MenuAdd" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script src="../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
    <script src="../Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {

            $.ajax({
                type: "post",
                url: "../Ajax/Menu_Manage.ashx",
                data: { "action": "GetGroupList" },
                dataType: "json",
                success: function (data) {
                    if (data != null && data != '') {
                        $.each(data, function (index, item) {
                            $("#selUserGroup").append('<option id="' + 'op' + (index + 1) + '"' + ' value="' + item.class + '">' + item.GroupName + '</option>');
                        });
                    }
                    else {
                        $("#selUserGroup").append('<option id="' + 'op' + '0' + '"' + ' value="' + '-1' + '">' + '无用户组' + '</option>');
                    }

                    var group = $("#selUserGroup").find("option:selected").text();
                    $.ajax({
                        type: "post",
                        url: "../Ajax/Menu_Manage.ashx",
                        data: { "action": "GetMenuList", "menuClass": "1", "group": group },
                        dataType: "json",
                        success: function (data) {
                            if (data != null && data != '') {
                                $("#sel1").append('<option id="' + 'op0' + '"' + ' value=-1' + '>' + '新建顶级菜单' + '</option>');
                                $.each(data, function (index, item) {
                                    $("#sel1").append('<option id="' + 'op' + (index + 1) + '"' + ' value="' + item.menuId + '">' + item.menuName + '</option>');
                                });
                            }
                            else {
                                $("#sel1").append('<option id="' + 'op0' + '"' + ' value=-1' + '>' + '新建顶级菜单' + '</option>');
                            }

                            var fatherId = $("#sel1").val();
                            if (fatherId != -1) {
                                $.ajax({
                                    type: "post",
                                    url: "../Ajax/Menu_Manage.ashx",
                                    data: { "action": "GetMenuList", "menuClass": "2", "fatherId": fatherId },
                                    dataType: "json",
                                    success: function (data) {
                                        $("#sel2").append('<option id="' + 'op0' + '" value=-1' + '>' + '新建二级菜单' + '</option>');
                                        if (data != null && data != '') {

                                            $.each(data, function (index, item) {
                                                $("#sel2").append('<option id="' + 'op' + (index + 1) + '"' + ' value="' + item.menuId + '">' + item.menuName + '</option>');
                                            });
                                        }
                                    }
                                });
                            }
                            $("#sel2").append('<option id="' + 'op0' + '" value=-1' + '>' + '无' + '</option>');
                        }
                    });
                }
            });

            $("#selUserGroup").change(function () {
                var group = $("#selUserGroup").find("option:selected").text();
                $("#sel1").html("");
                $.ajax({
                    type: "post",
                    url: "../Ajax/Menu_Manage.ashx",
                    data: { "action": "GetMenuList", "menuClass": "1", "group": group },
                    dataType: "json",
                    success: function (data) {
                        if (data != null && data != '') {
                            $("#sel1").append('<option id="' + 'op' + ('0') + '"' + ' value="' + '-1' + '">' + '新建一级菜单' + '</option>');
                            $.each(data, function (index, item) {
                                $("#sel1").append('<option id="' + 'op' + (index + 1) + '"' + ' value="' + item.menuId + '">' + item.menuName + '</option>');
                            });
                        }
                        else {
                            $("#sel1").append('<option id="' + 'op' + ('0') + '"' + ' value="' + '-1' + '">' + '新建一级菜单' + '</option>');
                        }
                    }
                });
            });

            $("#sel1").change(function () {

                $("#sel2").html("");
                var fatherId = $("#sel1").val();
                var group = $("#selUserGroup").find("option:selected").text();
                if (fatherId != -1) {
                    $.ajax({
                        type: "post",
                        url: "../Ajax/Menu_Manage.ashx",
                        data: { "action": "GetMenuList", "menuClass": "2", "fatherId": fatherId, "group": group },
                        dataType: "json",
                        success: function (data) {
                            $("#sel2").append('<option id="' + 'op0' + '" value=-1' + '>' + '新建二级菜单' + '</option>');
                            if (data != null && data != '') {
                                $.each(data, function (index, item) {
                                    $("#sel2").append('<option id="' + 'op' + (index + 1) + '"' + ' value="' + item.menuId + '">' + item.menuName + '</option>');
                                });
                            }
                        }
                    });
                }
                else {
                    $("#sel2").append('<option id="' + 'op0' + '" value=-1' + '>' + '无' + '</option>');
                }
            });

            $("#btnSave").click(function () {
                var fatherId = $("#sel1").val();
                var SecondMenuId = $("#sel2").val();
                var menuName = $("#tbMenuName").val();
                var url = $("#tbPageURL").val();
                var group = $("#selUserGroup").find("option:selected").text();
                var pageId = $("#tbPageId").val();
                var rightClass = $("#selUserGroup").val();
                alert(pageId);
                if (menuName.length > 0) {
                    $.ajax({
                        type: "post",
                        url: "../Ajax/Menu_Manage.ashx",
                        data: { "action": "MenuAdd", "fatherId": fatherId, "SecondMenuId": SecondMenuId, "menuName": menuName, "PageId": pageId, "url": url, "group": group, "rightClass": rightClass },
                        dataType: "json",
                        success: function (data) {
                            if (data["status"] == "dataduplicate") {
                                alert("该页面已存在");
                            }
                            else if (data["status"] == "updateErr") {
                                alert("菜单添加失败");
                            }
                            else if (data["status"] == "success") {
                                alert("菜单添加成功");
                                $("#saveSuc").css("display", "inherit");
                                $("#tb1").css("display", "none");
                            }
                        }
                    });
                }
                else {
                    alert("所有*为必填项");
                }
            });
            $("#btnCancel").click(function () {
                if (confirm("确认返回？")) {
                    window.history.back("MenuManage.aspx");
                }
            });
        });	
    </script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div id="saveSuc" style=" display:none;">
      <a href="/MenuManage/MenuManage.aspx">返回查询页面</a>
      <br />
      <br />
      <a href="/MenuManage/MenuAdd.aspx">继续添加页面</a>
    </div>
    <table id="tb1">
      <tr>
        <td><label>用户组</label></td>
        <td><select id="selUserGroup" style=" height:30px; line-height:30px; font-size:16px;"></select></td>
      </tr>
      <tr>
        <td><label>一级菜单</label></td>
        <td><select id="sel1" style=" height:30px; line-height:30px; font-size:16px;"></select></td>
      </tr>
      <tr>
        <td><label>二级菜单</label></td>
        <td><select id="sel2" style=" height:30px; line-height:30px; font-size:16px;"></select></td>
      </tr>
      <tr>
        <td><label>菜单名称</label></td>
        <td><input  type="text" id="tbMenuName" style=" height:30px; line-height:30px; font-size:16px; width:450px;"/></td>
        <td><label style=" color:Red;">*</label></td>
      </tr>
      <tr>
        <td><label>页面ID</label></td>
        <td><input  type="text" id="tbPageId" style=" height:30px; line-height:30px; font-size:16px; width:450px;"/></td>
        <td></td>
      </tr>
      <tr>
        <td><label>页面URL</label></td>
        <td><input  type="text" id="tbPageURL" value="/Folder/example.aspx?PageId=" style=" height:30px; line-height:30px; font-size:14px; width:450px;"/></td>
        <td></td>
      </tr>
      <tr>
        <td></td>
        <td>
          <input  type="button" id="btnSave" value="保存" style=" height:30px; line-height:30px; font-size:16px;"/>
          <input  type="button" id="btnCancel" value="取消" style=" height:30px; line-height:30px; font-size:16px;"/>
        </td>
      </tr>
    </table>  
    </div>
    </form>
</body>
</html>
